<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let year = 40;//总的投资年份
        let everyInto = 2;//每年投入2万
        let totalProfit = 0;
        let totalInto = 0;//总的投入，总本金
        const rate = 0.12;//每年收益率

        function profitFun (num) {
            return parseFloat((num * rate).toFixed(2))
        }
        
        // for (let i = 0; i < year; i++) {
        //     totalInto = totalInto + everyInto;
        //     let nowProfit = profitFun(totalInto);
        //     totalProfit = parseFloat((totalProfit + nowProfit).toFixed(2));

        //     console.log(`第${i+1}年投入：${everyInto}万, 总投入：${totalInto}万,年收益率是${rate}, 今年盈利：${nowProfit}万,把盈利取出,本金继续投。总的盈利：${totalProfit}万`);
        // }

        let preYearn = 0;//上一年的本金加收益
        // let totalProfit;
        function capProfit (num) {
            return parseFloat((num + num * rate).toFixed(2))
        }

        for (let i = 0; i < year; i++) {
            totalInto = totalInto + everyInto;

            // totalProfit = totalProfit + totalInto;

            let nowProfit = profitFun(totalInto);

            // let taotal = capProfit(everyInto + preYearn);
            // let taotal = nowProfit
            

            console.log(`第${i+1}年投入：${everyInto}万, 总账户：${totalInto}万,年收益率是${rate}, 今年盈利：${nowProfit}万,连本带利取出继续投。总的盈利：${totalProfit}万`);
            
            totalInto = totalInto + nowProfit;

        }


    </script>
</body>
</html>